package com.shanghaionstar.home.util;

import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.onstar.cn.common.util.Constants;
import com.onstar.cn.common.util.PreferencesUtls;
import com.onstar.cn.framework.mvp.base.core.BaseFragmentActivity;
import com.onstar.cn.weight.CustomImageView;
import com.shanghaionstar.R;

import java.util.List;

/**
 * Created by Rengui Lu on 2016/7/20.
 */
public class FloatingUtil {
    private BaseFragmentActivity mContext;
    private List<View> views;
    private PopupWindow popupWindow;
    private int popupIndex = 1;
    private CustomImageView imageView;
    private boolean isShowing = false;
    private Integer[] guideResourceIds = {R.drawable.floating_page1,
            R.drawable.floating_page2, R.drawable.floating_page3, R.drawable.floating_page4,
            R.drawable.floating_page5, R.drawable.floating_page6, R.drawable.floating_page7
    };

    public FloatingUtil(BaseFragmentActivity context) {
        this.mContext = context;
    }

    public FloatingUtil(BaseFragmentActivity context, List<View> views) {
        this.mContext = context;
        this.views = views;
    }

    private void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        mContext.getWindow().setAttributes(lp);
    }

    public void showLayer() {
        views.get(0).post(new Runnable() {
            @Override
            public void run() {
                if(!isShowing)
                    showFloatingLayer(views.get(1), guideResourceIds[1]);
                isShowing = true;
            }
        });
    }


    private void showFloatingLayer(View childView, int resId) {
        if (popupWindow == null) {
            backgroundAlpha(0.5f);
            imageView = new CustomImageView(mContext);
            imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            popupWindow = new PopupWindow(imageView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
            popupWindow.setContentView(imageView);
            popupWindow.setBackgroundDrawable(new BitmapDrawable());
            popupWindow.setAnimationStyle(com.onstar.cn.weight.R.style.floatinglayer_animation);

            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    popupIndex += 1;
                    if (popupIndex > 6) {
                        popupWindow.dismiss();
                        backgroundAlpha(1.0f);
                        PreferencesUtls.getInstance(mContext).put(Constants.FLOATING_GUIDANCE, true);
                        isShowing = false;
                    } else {
//                        if(popupIndex == 4)
//                            popupIndex += 1;
                        showFloatingLayer(views.get(popupIndex), guideResourceIds[popupIndex]);
                    }
                }
            });

            popupWindow.setTouchInterceptor(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                        mContext.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                popupWindow.dismiss();
                            }
                        });
                        return true;
                    }
                    return false;
                }
            });
        }
        imageView.setImageResource(resId);
        showPopupWindow(childView);
    }

    private void showFloating(View childView, int index) {
        imageView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int popupWidth = imageView.getMeasuredWidth();
        int popupHeight = imageView.getMeasuredHeight();
        int[] location = new int[2];
        childView.getLocationOnScreen(location);
        int childWidth = childView.getMeasuredWidth();
        int childHeight = childView.getMeasuredHeight();
        int[] data = {popupWidth, popupHeight, location[0], location[1], childWidth, childHeight};
        int[] offset = getOffset(index, data);
        popupWindow.showAtLocation(childView, Gravity.NO_GRAVITY, offset[0],
                offset[1]);
    }

    private int[] getOffset(int index, int[] datas) {
        int[] location = new int[2];
        switch (index) {
            case 3:
                location[0] = datas[2] + datas[4] / 2 - datas[0] / 2;
                location[1] = datas[3] - datas[1] + datas[5] / 4;
                break;
            case 4:
//                location[0] = datas[2] + datas[4] / 2 - datas[0] / 2;
                int screenWidth1 = mContext.getResources().getDisplayMetrics().widthPixels;
                location[0] = screenWidth1 * 5 / 8 - datas[0] / 2;
                location[1] = datas[3] - datas[1] + datas[5];
                break;
            case 5:
                int offY = (int) (25 * mContext.getResources().getDisplayMetrics().density);
                location[0] = datas[2] + datas[4] / 2 - datas[0] / 2;
                location[1] = datas[3] + (datas[5] - datas[1])/2;
                break;
            case 6:
                int screenWidth = mContext.getResources().getDisplayMetrics().widthPixels;
                location[0] = (screenWidth - datas[0]) / 2;
                location[1] = datas[3] + (datas[5] - datas[1]) / 2;
                break;
        }
        return location;
    }


    private void showPopupWindow(View childView) {
        switch (popupIndex) {
            case 0:
                int up = (int) (16 * mContext.getResources().getDisplayMetrics().density);
                popupWindow.showAsDropDown(childView, 0, -up);
                break;
            case 1:
            case 2:
                popupWindow.showAsDropDown(childView, 0, -childView.getMeasuredHeight() / 4);
                break;
            case 3:
                showFloating(childView, 3);
                break;
            case 4:
                showFloating(childView, 4);
                break;
            case 5:
                showFloating(childView, 5);
                break;
            case 6:
                showFloating(childView, 6);
                break;
        }
    }

//    imageView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
//    int popupHeight2 = imageView.getMeasuredHeight();
//    int popupWidth2 = imageView.getMeasuredWidth();
//    int childHeight = childView.getMeasuredHeight();
//    int childWidth = childView.getMeasuredWidth();
//    int screenWidth = getResources().getDisplayMetrics().widthPixels;
//    int[] location2 = new int[2];
//    childView.getLocationOnScreen(location2);
//    //                popupWindow.showAtLocation(childView, Gravity.NO_GRAVITY,(location2[0]+childView.getWidth()/2)-popupWidth2/2,-(popupHeight2+childHeight)/2);
////                int height = (int) (-93*getResources().getDisplayMetrics().density);
////                popupWindow.showAtLocation(childView, Gravity.NO_GRAVITY,getResources().getDisplayMetrics().widthPixels/2-popupWidth2/2,height);
//    int offY1 = (int) (25 * getResources().getDisplayMetrics().density);
//    int offY = location2[1] + childHeight + offY1;
//    int offX = screenWidth / 2 - popupWidth2 / 2;
//    int offX1 = (location2[0] + childView.getWidth() / 2) - popupWidth2 / 2;
//    popupWindow.showAtLocation(childView, Gravity.NO_GRAVITY, offX, offY);
//                popupWindow.showAsDropDown(childView,Gravity.NO_GRAVITY,offX/2,offY);

//    /**
//     * 添加引导图片
//     */
//    public void addGuideImage() {
//        View view  = getContentView();
//        if(view==null)return;
//        ViewParent viewParent = view.getParent().getParent().getParent();
//        boolean hasGuide = PreferencesUtls.getInstance(onActivity()).getBoolean(Constants.FLOATING_GUIDANCE,false);
//        if(viewParent instanceof FrameLayout){
//            final FrameLayout frameLayout = (FrameLayout)viewParent;
//            if(!hasGuide){//设置了引导图片
//                final ImageView guideImage = new ImageView(onActivity());
//                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
//                guideImage.setLayoutParams(params);
//                guideImage.setScaleType(ImageView.ScaleType.FIT_XY);
//                guideImage.setImageResource(guideResourceIds[clickTime]);
//                guideImage.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
////                        frameLayout.removeView(guideImage);
////                        MyPreferences.setIsGuided(getApplicationContext(), BasicActivity.this.getClass().getName());//设为已引导
//                        if(clickTime > 6){
//                            PreferencesUtls.getInstance(onActivity()).getBoolean(Constants.FLOATING_GUIDANCE,true);
//                            frameLayout.removeView(guideImage);
//                        }else{
//                            clickTime += 1;
//                            guideImage.setImageResource(guideResourceIds[clickTime]);
//                        }
//                    }
//                });
//                frameLayout.addView(guideImage);//添加引导图片
//            }
//        }
//    }


}
